package com.mathdroid.model.talents;

import java.util.HashMap;
import java.util.LinkedList;

import com.mathdroid.model.Model;
import com.mathdroid.model.enums.ESpec;
import com.mathdroid.model.enums.ETalentTree;

/**
 * This class is a talent specialization. It contains talents in talent trees 
 * and has a name.
 */
public class Spec 
extends HashMap<ETalentTree, HashMap<String, Talent>> {
	/**
	 * Generated UID.
	 */
	private static final long serialVersionUID = -2765452099118363082L;

	/**
	 * Points available at max level.
	 */
	private static final int MAX_POINTS = 41;
	
	/**
	 * The name of the template.
	 */
	private String name = "Unnamed";
	
	/**
	 * Get the name of this template.
	 * @return The name of this template.
	 */
	public final String getName() {
		return this.name;
	}
	
	/**
	 * Set the name of this template.
	 * @param n Name.
	 */
	public final void setName(final String n) {
		this.name = n;
	}
	
	/**
	 * Get the points spent in one talent tree.
	 * @param tree The tree you want to know more about.
	 * @return The points spent in the tree.
	 */
	public final int getTreePoints(final ETalentTree tree) {
		int points = 0;
		
		for (Talent t : this.get(tree).values()) {
			points = points + t.getPoints();
		}
		
		return points;
	}
	
	/**
	 * Get the points spent in all three talent trees for the current character 
	 * specialization.
	 * @return Total points spent.
	 */
	public final int getTotalPointsSpent() {
		int pointsSpent = 0;
		ESpec spec = Model.getESpec();
		
		for (ETalentTree t : Specializations.getTalentTreeEnums(spec)) {
			pointsSpent += this.getTreePoints(t);
		}
		
		return pointsSpent;
	}
	
	/**
	 * Get a specific talent tree from this model.
	 * @param tree The tree you want talents for.
	 * @return The talents in the tree.
	 */
	public final HashMap<String, Talent> getTree(final ETalentTree tree) {
		return this.get(tree);
	}
	
	@Override
	public final String toString() {
		LinkedList<ETalentTree> trees = 
			Specializations.getTalentTreeEnums(Model.getESpec());
		return this.name + " (" + this.getTreePoints(trees.get(0)) 
		+ "-" + this.getTreePoints(trees.get(1)) 
		+ "-" + this.getTreePoints(trees.get(2)) + ")";
	}

	/**
	 * Get the amount of points available at max level.
	 * @return Points at max level.
	 */
	public final int getMaxPoints() {
		return Spec.MAX_POINTS;
	}
}
